package com.mydomain.Bodega;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.faces.FacesMessages;

import com.mydomain.Bodega.PerfilListado.GadgetTypeConverter;

@Stateful
@Name("unidadDeMedidaListado")
@Scope(ScopeType.EVENT)
public class UnidadDeMedidaListado implements IUnidadMedidaListado {

	// All gadget types pulled from the database
	private List<InvUnidadMedida> mInvUnidadMedida;

	// Map to be used for select lists in the UI
	private Map<String, InvUnidadMedida> mInvUnidadMedidaMap;

	@PersistenceContext
	EntityManager em;

	/**
	 * When this bean is created, load up the types and populate the internal
	 * map
	 */
	@Create
	public void loadInvUnidadMedida() {
		// Load up the types from the database, ordering them by label
		mInvUnidadMedida = em.createQuery("select gt from InvUnidadMedida gt")
				.getResultList();

		// Convert the list into a map. We use a TreeMap in order
		// to preserve the ordering we asked for in the query
		// above.
		Map<String, InvUnidadMedida> results = new TreeMap<String, InvUnidadMedida>();

		for (InvUnidadMedida us : mInvUnidadMedida) {
			results.put(us.getUndNombre(), us);
		}

		mInvUnidadMedidaMap = results;
	}

	/*
	 * Accessor used in the UI
	 */
	public Map<String, InvUnidadMedida> getAllInvUnidadMedida() {
		return mInvUnidadMedidaMap;
	}

	/*
	 * Get a converter initialized with the current list of types
	 */
	public Converter getConverter() {
		return new GadgetTypeConverter(mInvUnidadMedida);
	}

	static public class GadgetTypeConverter implements Converter, Serializable {
		private List<InvUnidadMedida> mTypes;

		public GadgetTypeConverter(List<InvUnidadMedida> types) {
			this.mTypes = types;
		}

		public String getAsString(FacesContext ctx, UIComponent comp, Object obj) {
			if (obj == null)
				return null;

			InvUnidadMedida type = (InvUnidadMedida) obj;
			String val = String.valueOf(type.getUndCodigo());

			return val;
		}

		public Object getAsObject(FacesContext ctx, UIComponent comp,
				String strVal) throws ConverterException {
			if (strVal == null || strVal.length() == 0) {
				return null;
			}

			long id = Long.valueOf(strVal).longValue();
			for (InvUnidadMedida us : mTypes) {
				if (us.getUndCodigo() == id) {
					return us;
				}
			}

			return null;
		}
	}

	@Remove
	@Destroy
	public void destroy() {
	}

}
